﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.Electric.Common;

namespace HYCableTrayConnection
{
	public class HYTransitionFittingConnect : HYCmdEntry
	{
		public HYTransitionFittingConnect(ExternalCommandData commandData) : base(commandData)
		{
		}

		public void Execute()
		{
			try
			{
				IList<CableTray> list = new List<CableTray>();
				ISelectionFilter selectionFilter = new RevitFilter.SelectionCableTrayFilter();
				HYTransitionConnection hytransitionConnection = new HYTransitionConnection();
				for (;;)
				{
					list.Clear();
					RevitVersionFuncs.ClearSelection(this.m_sel);
					Reference reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第一根桥架");
					list.Add(this.m_doc.GetElement(reference) as CableTray);
					reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二根桥架,与第一根桥架平行");
					CableTray cableTray = this.m_doc.GetElement(reference) as CableTray;
					while (list[0].Id.IntegerValue == cableTray.Id.IntegerValue)
					{
						reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二根桥架,与第一根桥架平行");
						cableTray = (this.m_doc.GetElement(reference) as CableTray);
					}
					list.Add(cableTray);
					if (!hytransitionConnection.NewTransitionFitting(list))
					{
						TaskDialog.Show("盈建科提示", "连接错误，请检查桥架位置。");
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
			catch (MyException ex)
			{
				TaskDialog.Show("盈建科提示", "连接错误，" + ex.Message + "。");
			}
			catch (Exception)
			{
				TaskDialog.Show("盈建科提示", "连接错误，请检查桥架位置。");
			}
		}
	}
}
